using System;
using System.Windows.Forms;
using Eco.Windows.Forms;
using Eco.Handles;
using Descriptions_DomainModel.Model;
using CommonClasses.Logger;

namespace DomainModelEditor
{
	class Program
	{
        private static DefaultEcoSpace ecoSpace;

		/// <summary>
		/// The global EcoSpace instance.
		/// </summary>
		public static DefaultEcoSpace EcoSpace
		{
			get { return ecoSpace; }
		}

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
        /// 

		[STAThread]
		static void Main()
		{

            ILoggingManagement main_log;
            ILogger log;

            main_log = LoggerManager.CreateOnce(@".\LoggerManager.ih.config", @".\logs");
            log = main_log.GetLogger("DomainModelEditor");
            log.WriteMessage(MessagePriority.Info, "DomainModelEditor log session has begun");
                        
			Application.EnableVisualStyles();
			Application.SetCompatibleTextRenderingDefault(false);
			WinFormDequeuer.Active = true;
			Form mainForm = null;
			try
			{
				ecoSpace = new IDE.IDEEcoSpace(log);
				ecoSpace.Active = true;
                //CreateDomainModel();
                //var loader = new DomainModelLoader(ecoSpace);
                //loader.SaveDomainModelToFile("WorkDomainModel");
				mainForm = new DomainModelEditorForm(ecoSpace);

			}
			catch (Exception e)
			{
				new ThreadExceptionDialog(e).ShowDialog();
				throw;
			}
			Application.Run(mainForm);
			ecoSpace.Dispose();
		}

        public static void CreateDomainModel()
        {
            DomainModel DM = new DomainModel(ecoSpace);
            #region MP11;
            NodeGroup MP11 = new NodeGroup(ecoSpace) { Name = "MP11" };
            DM.NodeGroups.Add(MP11);
            #region UpperArm
            Node n1 = new Node(ecoSpace) { Name = "UpperArm", StatePinNumber = 0 };
            n1.StateSet.UpperBound = 1;
            n1.StateSet.BottomBound = 0;
            MP11.Nodes.Add(n1);

            Method m = new Method(ecoSpace) { Name = "Forward", IsModelledAlways = true, VirtualPinOut = 1, Signal = 1 };
            n1.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Backward", IsModelledAlways = true, VirtualPinOut = 2, Signal = 1 };
            n1.Methods.Add(m);

            Node n2 = new Node(ecoSpace) { Name = "Hand", StatePinNumber = 3 };
            n1.Nodes.Add(n2);
            n2.StateSet.UpperBound = 1;
            n2.StateSet.BottomBound = 0;
            m = new Method(ecoSpace) { Name = "Open", IsModelledAlways = true, VirtualPinOut = 4, Signal = 0 };
            n2.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Close", IsModelledAlways = true, VirtualPinOut = 5, Signal = 1 };
            n2.Methods.Add(m);

            Node n3 = new Node(ecoSpace) { Name = "Rotation",StatePinNumber = 6 };
            n2.Nodes.Add(n3);
            n3.StateSet.UpperBound = 1;
            n3.StateSet.BottomBound = 0;

            m = new Method(ecoSpace) { Name = "Left", IsModelledAlways = true, VirtualPinOut = 7, Signal = 1 };
            n3.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Right", IsModelledAlways = true, VirtualPinOut = 8, Signal = 1 };
            n3.Methods.Add(m);
            #endregion

            #region BottomArm
            n1 = new Node(ecoSpace) { Name = "BottomArm", StatePinNumber = 9 };
            MP11.Nodes.Add(n1);
            n1.StateSet.UpperBound = 1;
            n1.StateSet.BottomBound = 0;

            m = new Method(ecoSpace) { Name = "Forward", IsModelledAlways = true, VirtualPinOut = 10, Signal = 1 };
            n1.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Backward", IsModelledAlways = true, VirtualPinOut = 11, Signal = 1 };
            n1.Methods.Add(m);

            n2 = new Node(ecoSpace) { Name = "Hand", StatePinNumber = 12 };
            n1.Nodes.Add(n2);
            n2.StateSet.UpperBound = 1;
            n2.StateSet.BottomBound = 0;
            m = new Method(ecoSpace) { Name = "Open", IsModelledAlways = true, VirtualPinOut = 13, Signal = 0 };
            n2.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Close", IsModelledAlways = true, VirtualPinOut = 14, Signal = 1 };
            n2.Methods.Add(m);
            #endregion

            #region Table
            n1 = new Node(ecoSpace) { Name = "Table", StatePinNumber = 15 };
            MP11.Nodes.Add(n1);
            n1.StateSet.UpperBound = 1;
            n1.StateSet.BottomBound = 0;

            m = new Method(ecoSpace) { Name = "Up", IsModelledAlways = true, VirtualPinOut = 16, Signal = 1 };
            n1.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Down", IsModelledAlways = true, VirtualPinOut = 17, Signal = 0 };
            n1.Methods.Add(m);

            n2 = new Node(ecoSpace) { Name = "Rotation", StatePinNumber = 18 };
            n1.Nodes.Add(n2);
            n2.StateSet.UpperBound = 1;
            n2.StateSet.BottomBound = 0;
            m = new Method(ecoSpace) { Name = "Left", IsModelledAlways = true, VirtualPinOut = 19, Signal = 1 };
            n2.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Right", IsModelledAlways = true, VirtualPinOut = 20, Signal = 1 };
            n2.Methods.Add(m);
            #endregion
            #endregion

            #region mp9;
            NodeGroup mp9 = new NodeGroup(ecoSpace) { Name = "MP9" };
            DM.NodeGroups.Add(mp9);
            #region Arm
            n1 = new Node(ecoSpace) { Name = "Arm", StatePinNumber = 21 };
            mp9.Nodes.Add(n1);
            n1.StateSet.UpperBound = 1;
            n1.StateSet.BottomBound = 0;

            m = new Method(ecoSpace) { Name = "Forward", IsModelledAlways = true, VirtualPinOut = 22, Signal = 1 };
            n1.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Backward", IsModelledAlways = true, VirtualPinOut = 23, Signal = 1 };
            n1.Methods.Add(m);

            n2 = new Node(ecoSpace) { Name = "Hand", StatePinNumber=24 };
            n1.Nodes.Add(n2);
            n2.StateSet.UpperBound = 1;
            n2.StateSet.BottomBound = 0;
            m = new Method(ecoSpace) { Name = "Open", IsModelledAlways = true, VirtualPinOut = 25, Signal = 0 };
            n2.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Close", IsModelledAlways = true, VirtualPinOut = 26, Signal = 1 };
            n2.Methods.Add(m);
            #endregion

            #region Table
            n1 = new Node(ecoSpace) { Name = "Table", StatePinNumber=27 };
            mp9.Nodes.Add(n1);
            n1.StateSet.UpperBound = 1;
            n1.StateSet.BottomBound = 0;

            m = new Method(ecoSpace) { Name = "Up", IsModelledAlways = true, VirtualPinOut = 28, Signal = 1 };
            n1.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Down", IsModelledAlways = true, VirtualPinOut = 29, Signal = 0 };
            n1.Methods.Add(m);

            n2 = new Node(ecoSpace) { Name = "Rotation", StatePinNumber = 30 };
            n1.Nodes.Add(n2);
            n2.StateSet.UpperBound = 1;
            n2.StateSet.BottomBound = 0;
            m = new Method(ecoSpace) { Name = "Left", IsModelledAlways = true, VirtualPinOut = 31, Signal = 1 };
            n2.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Right", IsModelledAlways = true, VirtualPinOut = 32, Signal = 1 };
            n2.Methods.Add(m);
            #endregion
            #endregion

            #region Piston;
            NodeGroup piston = new NodeGroup(ecoSpace) { Name = "PISTON" };
            DM.NodeGroups.Add(piston);
            #region Lift
            n1 = new Node(ecoSpace) { Name = "Lift", StatePinNumber=33 };
            piston.Nodes.Add(n1);
            n1.StateSet.UpperBound = 1;
            n1.StateSet.BottomBound = 0;

            m = new Method(ecoSpace) { Name = "Up", IsModelledAlways = true, VirtualPinOut = 34, Signal = 1 };
            n1.Methods.Add(m);
            m = new Method(ecoSpace) { Name = "Down", IsModelledAlways = true, VirtualPinOut = 35, Signal = 1 };
            n1.Methods.Add(m);
            #endregion
            #endregion

        }
	}
}
